###############################################################################
#
# file: EditColorDialog.py
# author: Victor Bos
# 
# Copyright 2007--2011 Victor Bos
#
# This file is part of GREATOR.
#
#    GREATOR is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    GREATOR is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GREATOR.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import Tkinter
import tkColorChooser

CANCEL = 0
DELETED = 1
CHANGED = 2

class EditColourDialog(Tkinter.Toplevel):

    def __init__(self, master=None, **kw):
        self.process_options(kw)
        Tkinter.Toplevel.__init__(self, master, kw)
        self.protocol('WM_DELETE_WINDOW', self.cancel_pressed)
        self.title(self.windowtitle)

        lframe = Tkinter.LabelFrame(
            self, 
            text=self.message)
        lframe.pack(side = Tkinter.TOP, fill=Tkinter.BOTH, expand=1)

        f1 = self.create_main_frame(lframe)
        if f1 is not None:
            f1.pack(side = Tkinter.TOP, expand=1, fill=Tkinter.BOTH, padx=10, pady=10)

        f2 = self.create_ok_cancel_buttons()
        f2.pack(
            side = Tkinter.TOP, 
            anchor = Tkinter.SW, 
            fill = Tkinter.X, 
            expand=1)

        self.result = CANCEL
        self.center_on_master(master)


    def set_colours(self, fg, bg):
        self.fg_button.configure(background = fg)
        self.bg_button.configure(background = bg)

    def create_main_frame(self, lframe):
        f = Tkinter.Frame(lframe)

        fgl = Tkinter.Label(f, text = "Foreground")
        fgl.grid(row = 0, column = 0, sticky = "sw")
        self.fg_button = Tkinter.Button(
            f, 
            command = self.change_fg_button,
            width = 2)
        self.fg_button.grid(
            row = 0,
            column = 1,
            sticky = "w")

        bgl = Tkinter.Label(f, text = "Background")
        bgl.grid(row = 1, column = 0, sticky = "sw")
        self.bg_button = Tkinter.Button(
            f,
            command = self.change_bg_button,
            width =2)
        self.bg_button.grid(
            row = 1,
            column = 1,
            sticky = "w")

        return f
    

    def change_button_colour(self, button, msg):
        try:
            _, colour = tkColorChooser.askcolor(
                color = button["background"],
                parent = self,
                title = msg)
        except Tkinter.TclError:
            colour = None
        if ((colour is not None) and
            (colour != button["background"])):
            self.result = CHANGED
            button["background"] = colour

    def change_fg_button(self):
        self.change_button_colour(self.fg_button, "Define foreground colour")

    def change_bg_button(self):
        self.change_button_colour(self.bg_button, "Define background colour")
    
    def create_ok_cancel_buttons(self):
        f = Tkinter.Frame(self)

        self.delete_button = Tkinter.Button(
            f,
            text="Delete",
            command=self.delete_pressed)
        self.delete_button.pack(side=Tkinter.LEFT)

        Tkinter.Label(f).pack(side=Tkinter.LEFT, expand=1)

        self.ok_button = Tkinter.Button(
            f,
            text="Ok",
            command=self.ok_pressed)
        self.ok_button.pack(side=Tkinter.LEFT)

        self.cancel_button = Tkinter.Button(
            f,
            text="Cancel",
            command=self.cancel_pressed)
        self.cancel_button.pack(side=Tkinter.LEFT)
        return f
    
    def center_on_master(self, master):
        if master is not None:
            x = (master.winfo_rootx() +
                 ((master.winfo_width() - self.winfo_reqwidth()) / 2.0))
            y = (master.winfo_rooty() +
                 ((master.winfo_height() - self.winfo_reqheight()) / 2.0))
            self.geometry("+%d+%d" % (x,y))
        
    def process_options(self, kw):
        self.set_option("message", kw, "Edit unit colours")
        self.set_option("windowtitle", kw, "Edit unit colours")
        
    def set_option(self, name, user_values, default):
        if name in user_values:
            setattr(self, name, user_values[name])
            del user_values[name]
        else:
            setattr(self, name, default)

    def delete_pressed(self):
        self.result = DELETED
        self.quit()
        self.destroy()

    def ok_pressed(self):        
        self.quit()

    def get_fg(self):
        return self.fg_button["background"]

    def get_bg(self):
        return self.bg_button["background"]

    def cancel_pressed(self):
        self.result = CANCEL
        self.quit()
        self.destroy()

    def go(self):
        self.wait_visibility() # window needs to be visible for the grab
        self.grab_set()
        self.mainloop()
        return self.result
